﻿
// 2008-04-16  jgs Adding in Signal Stations



package com.unifiedfield {

	
// Import needed for Host Interface
import flash.external.ExternalInterface;
import flash.xml.XMLDocument
import flash.utils.*  // For Interval & Timer functions



public class cHost
	{

		
	// Static Variables for Host Interface
	private static var sCardId:String="";
  private static var hHostStartupTimer:Timer;
	private static var bHostReady=false;

	
	/**********************************
	*
	*  Init()
	*
	*  Initialize the application 
	*
	***********************************/
	public static function Init() 
		{
		// if ExternalInterface isn't available, there's nothing to do
		// so just note it and leave.
		if( !ExternalInterface.available ) 
			{
			trace( "External Interface *not* avaiable!" );
			return;
			}
		
		bHostReady=false;
		
		StartCommunicationEventHandlers();
		
		trace( "External Interface avaiable!" );
			
		} // End Init()
		
	/**********************************
	*
	*  StartCommunicationEventHandlers()
	*
	*  Start the hooks that will receive messages from host. 
	*
	***********************************/
	private static function StartCommunicationEventHandlers()
		{
			
		// expose a method "SetEnabled" to the Host
		ExternalInterface.addCallback( "SetEnabled" , SetEnabled );
					
		trace("SetEnabled");
		
		}	// End StartCommunicationEventHandlers()
	
	/**********************************
	*
	*  SetEnabled()
	*
	* 	Enable / disable the interface based on if the window
	* 	that the Flash Movie is hosted in has focus.  This
	* 	is a method that gets invoked directly from C#, and is
	* 	able to be invoked because of the ExternalInterface 
	*   addCallback in wireEventHandlers().
	*
	***********************************/
	private static function SetEnabled( enabled:String )
		{
		// enabled is a string coming from the C# program.  If enabled
		// is the string "true" then we'll set the buttons to be
		// enabled, otherwise disable the interface
	//	sweepstakes_btn.enabled = contact_btn.enabled = links_btn.enabled = ( enabled == "true" ) ? true : false;
		
		// Just for kicks, we'll return a value to the C# program - this
		// is just for demonstration purposes.
		
		bHostReady=false;
		
		trace("Enabled");
		
		return (1);
		} // End SetEnabled()
	
	/**********************************
	*
	*  convertXMLStringToObject()
	*
	* 	Responses from the host come in as XML strings
	*     (such as "<root><node label='test' /></root>")
	*     and need to be converted to XML Objects.
	*
	***********************************/
	public static function convertXMLStringToObject( str:String ):XMLDocument 
		{
		var xml:XMLDocument = new XMLDocument();
		var filesXmlString:String = str;
		xml.ignoreWhite = true;
		xml.parseXML( filesXmlString );
		return xml;
		} // End convertXMLStringToObject()
	
	/**********************************
	*
	*  OutToTrace()
	*
	* 	For testing, just sends given string out to trace.
	*
	***********************************/
	public static function OutToTrace(sMessage:String)
		{
		
		trace( sMessage );
	
		return;
		
		} // OutToTrace()
	
	/**********************************
	*
	*  ShowAlert()
	*
	* 	We ask the host to display a message box with our message.
	*
	***********************************/
	public static function ShowAlert(sMessage:String)
		{
		
		ExternalInterface.call( "OpenMessageBox" , sMessage );
	
		return;
		
		} // ShowAlert()
	
	/**********************************
	*
	*  AddToLog()
	*
	* 	We ask the host to add an entry to the FLASH log file.
	*   This will only do something of the FLASH log file setting
	*      exists in .INI file of the host application.
	*
	***********************************/
	public static function AddToLog(sMessage:String)
		{
			
			trace( sMessage );
		
		ExternalInterface.call( "AddToLog" , sMessage );
	
		return;
		
		} // AddToLog()
		
	/**********************************
	*
	*  GetConfigurationSetting()
	*
	* 	We ask the host to add an entry to the FLASH log file.
	*   This will only do something of the FLASH log file setting
	*      exists in .INI file of the host application.
	*
	***********************************/
	public static function GetConfigurationSetting(sSection:String, sKey:String):String
		{

		var sSetting:String;

		sSetting = String( ExternalInterface.call( "GetConfig" , "Bob" ));
	
		trace( "Get Config" );
	
		return(sSetting);
		
		} // GetConfigurationSetting()
		
	/**********************************
	*
	*  GetDiskSpace()
	*
	* 	We ask the host for the time.
	*
	***********************************/
	public static function GetDiskSpace():String 
		{
			
		var sSpace:String;
		
		// Note the synchronous communication here - we make a call to the C# container, and get
		// a String response back.  Then, we convert that string response to an XML object and
		// use it as a dataProvider for the tree.
//		sSpace = String( ExternalInterface.call( "GetDiskSpace" , "C" ) );
	
		sSpace = String( ExternalInterface.call( "GetConfig" , "Primero", "Second" ));
	
		return(sSpace);
		
		} // GetDiskSpace()
	
	/**********************************
	*
	*  GetTime()
	*
	* 	We ask the host for the time.
	*
	***********************************/
	public static function GetTime():String 
		{
			
		var sTime:String;
		
		// Note the synchronous communication here - we make a call to the C# container, and get
		// a String response back.  Then, we convert that string response to an XML object and
		// use it as a dataProvider for the tree.
		sTime = String( ExternalInterface.call( "GetTime" , "C" ) );
	
		return(sTime);
		
		} // GetTime()
	
	/**********************************
	*
	*  FelicaGetLastError()
	*
	* 	Retrives a string that describes the last Felica Error that occurred.
	*
	***********************************/
	public static function FelicaGetLastError():String 
		{
		
		var sError:String;
		
		sError = String( ExternalInterface.call( "Felica_GetLastError" , "C" ) );
	
		return(sError);
		
		} // FelicaGetLastError()
		
	/**********************************
	*
	*  FelicaOpen()
	*
	* 	Connect to the Felica Reader.
	*
	***********************************/
	public static function FelicaOpen():String 
		{
	
		var sResult:String;
	
		sResult = String( ExternalInterface.call( "Felica_Open" , "C" ) );
	
		return(sResult);
		
		} // FelicaOpen()
	
	/**********************************
	*
	*  FelicaClose()
	*
	* 	We ask the host for the available disk space.
	*
	***********************************/
	public static function FelicaClose():String 
		{
		
		var sResult:String;
		
		sResult = String( ExternalInterface.call( "Felica_Close" , "C" ) );
	
		return(sResult);
		
		} // FelicaClose()
	
	/**********************************
	*
	*  FelicaStartPolling()
	*
	* 	We ask the host for the available disk space.
	*
	***********************************/
	public static function FelicaStartPolling():String 
		{
		
		var sResult:String;
		var hFelicaPollingTimer:Timer	
		var sId:String;
		
		sResult = String( ExternalInterface.call( "Felica_StartPolling" , "C" ) );
		
		return(sResult);
		
		} // FelicaStartPolling()
		
	/**********************************
	*
	*  FelicaStopPolling()
	*
	* 	We ask the host for the available disk space.
	*
	***********************************/
	public static function FelicaStopPolling():String 
		{
		
		var sResult:String;
		
		//killTimer(intervalId);
		
		sResult = String( ExternalInterface.call( "Felica_StopPolling" , "C" ) );
	
		return(sResult);
		
		} // FelicaStopPolling()
		
	/**********************************
	*
	*  FelicaCheckForCard()
	*
	* 	We check host to see if a card has been seen since the last
	*        time we asked.
	*   If card was seen we card back the "ID" of the card.
	*     Otherwise we get "0".
	*
	***********************************/
	public static function FelicaCheckForCard():String 
		{
		
		var sId:String;
		
		sId = String( ExternalInterface.call( "Felica_CheckForCard" , "C" ) );
		
		if( sId != "0" )
			{
			sCardId=sId;
			}
				
		return(sId);
		
		} // FelicaCheckForCard()
		
	/**********************************
	*
	*  FelicaGetCurrentUserId()
	*
	* 	Returns the last User ID captured form a guest's card.
	*
	***********************************/
	public static function FelicaGetCurrentUserId():String 
		{
		
		return( sCardId );
		
		} // FelicaGetCurrentUserId()
		
	/**********************************
	*
	*  WebCamOpen()
	*
	* 	Starts up the web cam
	*
	***********************************/
	public static function WebCamOpen():String 
		{
		
		var sResult:String;
		
		sResult = String( ExternalInterface.call( "WebCam_Open" , "C" ) );
		
		//if( sId != "0" )
			//{
			//sCardId=sId;
			//}
				
		return(sResult);
		
		} // WebCamOpen()		
		
	/**********************************
	*
	*  WebCamShow()
	*
	* 	Shows the web cam
	*
	***********************************/
	public static function WebCamShow() 
		{
		
		String( ExternalInterface.call( "WebCam_Show" , "C" ) );
		
		} // WebCamShow()		
		
	/**********************************
	*
	*  WebCamHide()
	*
	* 	Hides the web cam
	*
	***********************************/
	public static function WebCamHide() 
		{
		
		String( ExternalInterface.call( "WebCam_Hide" , "C" ) );
		
		} // WebCamHide()		
		
	/**********************************
	*
	*  WebCamClose()
	*
	* 	Closes the web cam
	*
	***********************************/
	public static function WebCamClose() 
		{
		
		String( ExternalInterface.call( "WebCam_Close" , "C" ) );
		
		} // WebCamClose()		
		
	/**********************************
	*
	*  WebCamSnap()
	*
	* 	Takes a picture
	*
	***********************************/
	public static function WebCamSnap() 
		{
		
		String( ExternalInterface.call( "WebCam_Snap" , "C" ) );
		
		} // WebCamSnap()		

	/**********************************
	*
	*  CameraConnect()
	*
	* 	Connects to the Digital Camera
	*
	***********************************/
	public static function CameraConnect() 
		{
		
		var sResult:String;
		
//trace( "Start Camera Connect!" );
		String( ExternalInterface.call( "Camera_Connect" , "C" ) );
//trace( "End Camera Connect!" );
		
		} // CameraConnect()		
		
	/**********************************
	*
	*  CameraDisconnect()
	*
	* 	Disconnects from the Digital Camera
	*
	***********************************/
	public static function CameraDisconnect() 
		{
		
		String( ExternalInterface.call( "Camera_Disconnect" , "C" ) );
		
		} // CameraDisconnect()		

	/**********************************
	*
	*  CameraShow()
	*
	* 	Shows the viewfinder on the Digital Camera
	*
	***********************************/
	public static function CameraShow() 
		{
		
		String( ExternalInterface.call( "Camera_Show" , "C" ) );
		
		} // CameraShow()		
		
	/**********************************
	*
	*  CameraHide()
	*
	* 	Hides the viewfinder on the Digital Camera
	*
	***********************************/
	public static function CameraHide() 
		{
		
		String( ExternalInterface.call( "Camera_Hide" , "C" ) );
		
		} // CameraHide()		
		
	/**********************************
	*
	*  CameraTakePicture()
	*
	* 	Takes the picture and gives it the given name.
	*
	***********************************/
	public static function CameraTakePicture() 
		{
		
		String( ExternalInterface.call( "Camera_TakePicture" , "C" ) );
		
		} // CameraTakePicture()		
				
	/**********************************
	*
	*  CameraShowPicture()
	*
	* 	Displays the last picture taken.
	*
	***********************************/
	public static function CameraShowPicture() 
		{
		
		String( ExternalInterface.call( "Camera_ShowPicture" , "C" ) );
		
		} // CameraShowPicture()		
				
	/**********************************
	*
	*  CameraHidePicture()
	*
	* 	Hides the last picture taken.
	*
	***********************************/
	public static function CameraHidePicture() 
		{
		
		String( ExternalInterface.call( "Camera_HidePicture" , "C" ) );
		
		} // CameraHidePicture()		
		
	/**********************************
	*
	*  Signal_ImageLoad()
	*
	* 	Loads the image for signal stations.
	*
	***********************************/
	public static function  Signal_ImageLoad() 
		{
		
		String( ExternalInterface.call( "SignalImage_Load" , "C" ) );
		
		} //  Signal_ImageLoad()		

	/**********************************
	*
	*  Signal_ImageShow()
	*
	* 	Shows the signal station image
	*
	***********************************/
	public static function  Signal_ImageShow() 
		{
		
		String( ExternalInterface.call( "SignalImage_Show" , "C" ) );
		
		} //  Signal_ImageLoad()		
		
	/**********************************
	*
	*  Signal_ImageHide()
	*
	* 	Hides the signal station image
	*
	***********************************/
	public static function  Signal_ImageHide() 
		{
		
		String( ExternalInterface.call( "SignalImage_Hide" , "C" ) );
		
		} //  Signal_ImageHide()		

	/**********************************
	*
	*  Signal_ImageRestart()
	*
	* 	Restarts the Signal Station Image Interface
	*
	***********************************/
	public static function  Signal_ImageRestart() 
		{
		
		String( ExternalInterface.call( "SignalImage_Restart" , "C" ) );
		
		} //  Signal_ImageRestart()		

	/**********************************
	*
	*  Signal_ImageUndo()
	*
	* 	Performs UNDO restoring image to it's last state.
	*   There is only one UNDO level.
	*   "Stamp" and Color are not affected by UNDO.
	*
	***********************************/
	public static function  Signal_ImageUndo() 
		{
		
		String( ExternalInterface.call( "SignalImage_Undo" , "C" ) );
		
		} //  Signal_ImageUndo()		

	/**********************************
	*
	*  Signal_ImageSave()
	*
	* 	Saves the image to a preset filename and path.
	*
	***********************************/
	public static function  Signal_ImageSave() 
		{
		
		String( ExternalInterface.call( "SignalImage_Save" , "C" ) );
		
		} //  Signal_ImageSave()		

 public static var SIGNAL_EFX_INVERT:Number = 1;
 public static var SIGNAL_EFX_GRAY_SCALE:Number = 2;
 public static var SIGNAL_EFX_EMBOSS:Number = 3;
 public static var SIGNAL_EFX_KIRSCH:Number = 4;
 public static var SIGNAL_EFX_FLIP:Number = 5;
 public static var SIGNAL_EFX_SWIRL:Number = 6;
 public static var SIGNAL_EFX_SPHERE:Number = 7;
 public static var SIGNAL_EFX_WARP:Number = 8;
 public static var SIGNAL_EFX_WATER:Number = 9;
 public static var SIGNAL_EFX_PIXELATE:Number = 10;
 public static var SIGNAL_EFX_COLOR:Number = 11;
 public static var SIGNAL_EFX_STAMP:Number = 12;

	/**********************************
	*
	*  Signal_ImageEfx()
	*
	* 	Does the specified Effect
	*
	***********************************/
	public static function  Signal_ImageEfx( iEfx:Number, iParam1:Number, iParam2:Number, iParam3:Number ) 
		{
		
		String( ExternalInterface.call( "SignalImage_Efx" , iEfx, iParam1, iParam2, iParam3 ) );
		
		} //  Signal_ImageEfx()		
		

			
				
	} // End of class cHost
	
}  // End of Package

